#!/usr/bin/env python 
# -*- coding: utf-8 -*-
import math
from point import Point

#Класс-контейнер для окружности, одновременно и ищет окружность по трем точкам
class Circle:
    def __init__(self):
        self.m_dRadius = -1
        self.m_Center = None
        self.start_angel = None
        self.end_angel = None
        
    def get_m_dRadius(self):
        return self.m_dRadius
    
    def get_start_angel(self):
        return self.start_angel
    
    def get_end_angel(self):
        return self.end_angel
    
    def get_m_Center(self):
        return self.m_Center
    
    def make_circle(self, pt1, pt2, pt3):
        self.m_dRadius=-1
        if not self.isPerpendicular(pt1, pt2, pt3):
            self.calcCircle(pt1, pt2, pt3)    
        elif not self.isPerpendicular(pt1, pt3, pt2):
            self.calcCircle(pt1, pt3, pt2)    
        elif not self.isPerpendicular(pt2, pt1, pt3):
            self.calcCircle(pt2, pt1, pt3)    
        elif not self.isPerpendicular(pt2, pt3, pt1):        
            self.calcCircle(pt2, pt3, pt1)    
        elif not self.isPerpendicular(pt3, pt2, pt1):        
            self.calcCircle(pt3, pt2, pt1)    
        elif not self.isPerpendicular(pt3, pt1, pt2):        
            self.calcCircle(pt3, pt1, pt2)    
  
    def isPerpendicular(self, pt1, pt2, pt3):
        yDelta_a = pt2.get_y()-pt1.get_y()
        xDelta_a = pt2.get_x()-pt1.get_x()
        yDelta_b = pt3.get_y()-pt2.get_y()
        xDelta_b = pt3.get_x()-pt2.get_x()
        
        # checking whether the line of the two pts are vertical
        if (xDelta_a == 0 and yDelta_b == 0):
            return False
        if yDelta_a == 0 or yDelta_b == 0 or xDelta_a == 0 or xDelta_b == 0:
            return True;
        else:
            return False
            
    def calcCircle(self, pt1, pt2, pt3):
        yDelta_a = float(pt2.get_y()-pt1.get_y())
        xDelta_a = float(pt2.get_x()-pt1.get_x())
        yDelta_b = float(pt3.get_y()-pt2.get_y())
        xDelta_b = float(pt3.get_x()-pt2.get_x())
        
        if (xDelta_a == 0 and yDelta_b == 0):
            self.m_Center = Point(0.5*(pt2.get_x() + pt3.get_x()), 0.5*(pt1.get_y() + pt2.get_y()))
            self.m_dRadius = math.sqrt((pt1.get_x() - self.m_Center.get_x())**2 + (pt1.get_y() - self.m_Center.get_y())**2)
            return self.m_dRadius
        
        aSlope = yDelta_a/xDelta_a
        bSlope = yDelta_b/xDelta_b
        if aSlope-bSlope == 0:    # checking whether the given points are colinear.     
            return -1;
        # calc center
        x = (aSlope*bSlope*(pt1.get_y()-pt3.get_y())+bSlope*(pt1.get_x()+pt2.get_x())-aSlope*(pt2.get_x()+pt3.get_x()))/(2*(bSlope-aSlope))
        y = (-1*(x - (pt1.get_x()+pt2.get_x())/2)/aSlope+(pt1.get_y()+pt2.get_y())/2)
        self.m_Center = Point(x, y)
        self.m_dRadius = math.sqrt((pt1.get_x() - self.m_Center.get_x())**2 + (pt1.get_y() - self.m_Center.get_y())**2)
        self.set_angels(pt1, pt2)
        return self.m_dRadius

    def set_angels(self, pt1, pt2):
        x_delta_pt1 = float(pt1.get_x()-self.m_Center.get_x())
        y_delta_pt1 = float(pt1.get_y()-self.m_Center.get_y())
        x_delta_pt2 = float(pt2.get_x()-self.m_Center.get_x())
        y_delta_pt2 = float(pt2.get_y()-self.m_Center.get_y())
        
        #первая четверть
        if x_delta_pt1 > 0 and y_delta_pt1 < 0:
            self.start_angel = abs(math.atan(y_delta_pt1/x_delta_pt1))
        if x_delta_pt2 > 0 and y_delta_pt2 < 0:
            self.end_angel = abs(math.atan(y_delta_pt2/x_delta_pt2))
        #вторая четверть
        if x_delta_pt1 < 0 and y_delta_pt1 < 0:
            self.start_angel = abs(math.pi - math.atan(y_delta_pt1/x_delta_pt1))
        if x_delta_pt2 < 0 and y_delta_pt2 < 0:
            self.end_angel = abs(math.pi - math.atan(y_delta_pt2/x_delta_pt2))
        #третья четверть
        if x_delta_pt1 < 0 and y_delta_pt1 > 0:
            self.start_angel = abs(math.pi + math.atan(y_delta_pt1/x_delta_pt1))
        if x_delta_pt2 < 0 and y_delta_pt2 > 0:
            self.end_angel = abs(math.pi + math.atan(y_delta_pt2/x_delta_pt2))
        #четвертая четверть
        if x_delta_pt1 > 0 and y_delta_pt1 > 0:
            self.start_angel = abs(2*math.pi - math.atan(y_delta_pt1/x_delta_pt1))
        if x_delta_pt2 > 0 and y_delta_pt2 > 0:
            self.end_angel = abs(2*math.pi - math.atan(y_delta_pt2/x_delta_pt2))
        
        
        